package com.ruoyi.cms.service.impl;

import java.util.List;

import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.cms.mapper.CmsCatalogMapper;
import com.ruoyi.cms.domain.CmsCatalog;
import com.ruoyi.cms.service.ICmsCatalogService;

import javax.annotation.PostConstruct;

/**
 * 栏目信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-02-01
 */
@Service
public class CmsCatalogServiceImpl implements ICmsCatalogService 
{
    private static final String CACHE_KEY = "cms_catalog";

    @Autowired
    private RedisCache redisCache;

    @Autowired
    private CmsCatalogMapper cmsCatalogMapper;

    @PostConstruct
    public  int recache(){
        redisCache.deleteObject(CACHE_KEY);
        List<CmsCatalog> list = cmsCatalogMapper.selectCmsCatalogList(null);
        for(CmsCatalog catalog:list){
            redisCache.setCacheMapValue(CACHE_KEY, "id_"+catalog.getId(), catalog);
            redisCache.setCacheMapValue(CACHE_KEY, "code_"+catalog.getCode(), catalog.getId());
        }
        return 0;
    }

    /**
     * 查询栏目信息
     *
     * @param code 栏目信息code
     * @return 栏目信息
     */
    public CmsCatalog selectCmsCatalogByCode(String code){
        Long id = redisCache.getCacheMapValue(CACHE_KEY, "code_" + code);
        return selectCmsCatalogById(id);
    }

    /**
     * 查询栏目信息
     * 
     * @param id 栏目信息主键
     * @return 栏目信息
     */
    @Override
    public CmsCatalog selectCmsCatalogById(Long id)
    {
        return redisCache.getCacheMapValue(CACHE_KEY, "id_"+id);
    }

    /**
     * 查询栏目信息列表
     * 
     * @param cmsCatalog 栏目信息
     * @return 栏目信息
     */
    @Override
    public List<CmsCatalog> selectCmsCatalogList(CmsCatalog cmsCatalog)
    {
        return cmsCatalogMapper.selectCmsCatalogList(cmsCatalog);
    }

    /**
     * 新增栏目信息
     * 
     * @param cmsCatalog 栏目信息
     * @return 结果
     */
    @Override
    public int insertCmsCatalog(CmsCatalog cmsCatalog)
    {
        cmsCatalog.setCreateTime(DateUtils.getNowDate());
        cmsCatalog.setCreateBy(SecurityUtils.getUsername());
        return cmsCatalogMapper.insertCmsCatalog(cmsCatalog) + recache();
    }

    /**
     * 修改栏目信息
     * 
     * @param cmsCatalog 栏目信息
     * @return 结果
     */
    @Override
    public int updateCmsCatalog(CmsCatalog cmsCatalog)
    {
        cmsCatalog.setUpdateTime(DateUtils.getNowDate());
        cmsCatalog.setUpdateBy(SecurityUtils.getUsername());
        return cmsCatalogMapper.updateCmsCatalog(cmsCatalog) + recache();
    }

    /**
     * 批量删除栏目信息
     * 
     * @param ids 需要删除的栏目信息主键
     * @return 结果
     */
    @Override
    public int deleteCmsCatalogByIds(Long[] ids)
    {
        return cmsCatalogMapper.deleteCmsCatalogByIds(ids) + recache();
    }

    /**
     * 删除栏目信息信息
     * 
     * @param id 栏目信息主键
     * @return 结果
     */
    @Override
    public int deleteCmsCatalogById(Long id)
    {
        return cmsCatalogMapper.deleteCmsCatalogById(id) + recache();
    }
}
